package com.xg.sys.service.impl;

import com.xg.sys.entity.SysMenu;
import com.xg.sys.entity.SysUser;
import com.xg.sys.mapper.SysMenuMapper;
import com.xg.sys.mapper.SysUserMapper;
import com.xg.sys.mapper.SysUserTokenMapper;
import com.xg.sys.service.ShiroService;
import com.xg.frame.constant.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: chenliang
 * @Date: 2019/4/28 10:17
 * @Version 1.0
 */
@Service
public class ShiroServiceImpl implements ShiroService {
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysUserTokenMapper sysUserTokenMapper;
    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public Set<String> getSysUserPerms(String userId) {
        List<String> permsList;
        if (userId.equals(Constant.SUPPER_ADMIN)) {
            List<SysMenu> menuList = sysMenuMapper.selectList(null);
            permsList = new ArrayList<>(menuList.size());
            for (SysMenu sysMenu : menuList) {
                permsList.add(sysMenu.getPerms());
            }
        } else {
            permsList = sysUserMapper.getAllPerms(userId);
        }
        //用户权限列表
        HashSet<String> permsSet = new HashSet<>();

        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    @Override
    public SysUser getUserByToken(String token) {
        return null;
    }

    @Override
    public SysUser getUserById(String userId) {
        return null;
    }
}
